package edu.columbia.ubms.ejb;

import static org.junit.Assert.*;

import org.junit.Before;
import org.junit.Test;
import java.io.*;

public class UserInfoSessionBeanTest {

	private UserInfoSessionBean user1,user2;
	
	@Before
	public void setUp() throws Exception {
		user1 = new UserInfoSessionBean();
		user2 = new UserInfoSessionBean();
	}

	@Test
	public void testUserRegister() {
		
		String userName = "";
		String firstName = "";
		String lastName = "";
		String passWord = "";
		String email = "";
		
		int userId = 0;
		
		//////////////////////////////////////////////////////////////////////////////////////////
		/* 
		 * test cases for userName argument
		 *
		 */
		
		
		/* 
		 * test a valid userName
		 */
		userName = "jc3221";
		userId = user2.userRegister(userName, firstName, lastName, passWord, email);
		assertTrue(userId > 0);
		
		
		/*
		 * test a null input
		 */
		userName = null;
		userId = user2.userRegister(userName, firstName, lastName, passWord, email);
		assertFalse(userId > 0);
		
		
		/*
		 * test an empty string
		 */
		userName = "";
		userId = user2.userRegister(userName, firstName, lastName, passWord, email);
		assertFalse(userId > 0);
		
		
		
		/*
		 * test a string that contains only spaces
		 */
		userName = "                ";
		userId = user2.userRegister(userName, firstName, lastName, passWord, email);
		assertFalse(userId > 0);
	
		
		/*
		 * test a string with 16 characters
		 */
		userName = "1234567890123456";
		userId = user2.userRegister(userName, firstName, lastName, passWord, email);
		assertFalse(userId > 0);
		
		
		/*
		 * test a string with 17 characters
		 */
		userName = "12345678901234567";
		userId = user2.userRegister(userName, firstName, lastName, passWord, email);
		assertFalse(userId > 0);
		
		
		/*
		 * test a string with invalid characters
		 */
		userName = "&*()w*(>";
		userId = user2.userRegister(userName, firstName, lastName, passWord, email);
		assertFalse(userId > 0);
		
		
		/*
		 * test a string containing SQL command
		 */
		userName = "delete from userInfo";
		userId = user2.userRegister(userName, firstName, lastName, passWord, email);
		assertFalse(userId > 0);
		
		
		
		//////////////////////////////////////////////////////////////////////////////////////////
		/* 
		 * test cases for firstName argument (test cases for lastName and passWord arguments would be the same)
		 *
		 */
		
		userName = "jc3221";
		
		/* 
		 * test a valid firstName
		 */
		firstName = "jia";
		userId = user2.userRegister(userName, firstName, lastName, passWord, email);
		assertTrue(userId > 0);
		
		
		/*
		 * test a null input
		 */
		firstName = null;
		userId = user2.userRegister(userName, firstName, lastName, passWord, email);
		assertFalse(userId > 0);
		
		
		/*
		 * test an empty string
		 */
		firstName = "";
		userId = user2.userRegister(userName, firstName, lastName, passWord, email);
		assertFalse(userId > 0);
		
		
		/*
		 * test a string that contains only spaces
		 */
		firstName = "                ";
		userId = user2.userRegister(userName, firstName, lastName, passWord, email);
		assertFalse(userId > 0);
	
		
		
		/*
		 * test a string with 16 characters
		 */
		firstName = "1234567890123456";
		userId = user2.userRegister(userName, firstName, lastName, passWord, email);
		assertFalse(userId > 0);
		
		
		/*
		 * test a string with 17 characters
		 */
		firstName = "12345678901234567";
		userId = user2.userRegister(userName, firstName, lastName, passWord, email);
		assertFalse(userId > 0);
		
		
		/*
		 * test a string with invalid characters
		 */
		firstName = "&*()w*(>";
		userId = user2.userRegister(userName, firstName, lastName, passWord, email);
		assertFalse(userId > 0);
		
		
		/*
		 * test a string containing SQL command
		 */
		firstName = "delete from userInfo";
		userId = user2.userRegister(userName, firstName, lastName, passWord, email);
		assertFalse(userId > 0);
		
		
		
		//////////////////////////////////////////////////////////////////////////////////////////
		/* 
		 * test cases for email address
		 *
		 */
		
		userName = "jc3221";
		firstName = "jia";
		lastName = "chen";
		passWord = "abcd1234";
		
		/* 
		 * test a valid email
		 */
		email = "jc3221@columbia.edu";
		userId = user2.userRegister(userName, firstName, lastName, passWord, email);
		assertTrue(userId > 0);
		
		
		/*
		 * test a null input
		 */
		email = null;
		userId = user2.userRegister(userName, firstName, lastName, passWord, email);
		assertFalse(userId > 0);
		
		
		/*
		 * test an empty string
		 */
		email = "";
		userId = user2.userRegister(userName, firstName, lastName, passWord, email);
		assertFalse(userId > 0);
		
		
		/*
		 * test a string that contains only spaces
		 */
		email = "                ";
		userId = user2.userRegister(userName, firstName, lastName, passWord, email);
		assertFalse(userId > 0);
	
		
		
		
		/*
		 * test a string with 30 characters
		 */
		email = "1234567890123456@gmail.com";
		userId = user2.userRegister(userName, firstName, lastName, passWord, email);
		assertFalse(userId > 0);
			
		

		/*
		 * test a string without any character after @
		 */
		email = "123456734567@";
		userId = user2.userRegister(userName, firstName, lastName, passWord, email);
		assertFalse(userId > 0);
		
		
		/*
		 * test a string with more than one occurrence of @
		 */
		email = "1234@56734567@";
		userId = user2.userRegister(userName, firstName, lastName, passWord, email);
		assertFalse(userId > 0);
		
		
		/*
		 * test a string with invalid characters
		 */
		email = "&*()w*(>--++==";
		userId = user2.userRegister(userName, firstName, lastName, passWord, email);
		assertFalse(userId > 0);
		
		
		/*
		 * test a string containing SQL command
		 */
		email = "delete from userInfo";
		userId = user2.userRegister(userName, firstName, lastName, passWord, email);
		assertFalse(userId > 0);
		
		
		
		
		
		
		fail("Not yet implemented");
	}

	@Test
	public void testUserDupCheck() {
		
		boolean result;
		
		/**
		 * test a valid userName that exists in DB
		 */		
		result = user1.userDupCheck("jc3221");
		assertTrue("A valid username didn't get returned!",result);
		
		
		/*
		 * test a valid userName that doesn't exist in DB
		 */
		result = user1.userDupCheck("username");
		assertFalse(result);
		
		
		/*
		 * test a null input
		 */
		result = user1.userDupCheck(null);
		assertFalse(result);
		
		
		/*
		 * test an empty string
		 */
		result = user1.userDupCheck("");
		assertFalse(result);
		
		
		
		/*
		 * test a string that contains only spaces or other unprinted characters
		 */
		result = user1.userDupCheck("  \t  \n");
		assertFalse(result);
		
				
		
		/*
		 * test a string that contains 1k characters
		 */
		
		//prepare data
		String fileName = System.getProperty("user.home") + File.separator + ".test";
		try{
			BufferedReader in = new BufferedReader(new FileReader(fileName));
			String content ="";
			String line;
			while((line = in.readLine()) != null)
			{
				content += line;
			}
			result = user1.userDupCheck(content);
			assertFalse(result);
		}
		catch(IOException e)
		{
			System.err.println(e.getMessage());
		}
		
		
		
		/*
		 * test a string containing international characters;
		 */		
		result = user1.userDupCheck("&*08908$#% 赵钱孙李");
		assertFalse(result);
		
		
		/*
		 * test a string containing SQL command;
		 */
		String sqlQuery = "jc3221 OR username in (select * from userinfo)";
		result = user1.userDupCheck(sqlQuery);
		assertFalse(result);
		
		
	
		//fail("Not yet implemented");
	}

}
